package com.hema.auction.fragment;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.google.gson.Gson;
import com.hema.auction.R;
import com.hema.auction.activity.AddressActivity;
import com.hema.auction.activity.BindPhoneActivity;
import com.hema.auction.activity.DifferenceOrderActivity;
import com.hema.auction.activity.MessageActivity;
import com.hema.auction.activity.MyAuctionActivity;
import com.hema.auction.activity.MyInfoActivity;
import com.hema.auction.activity.MyPropertyActivity;
import com.hema.auction.activity.RechargeActivity;
import com.hema.auction.activity.SettingActivity;
import com.hema.auction.activity.SunSquareActivity;
import com.hema.auction.base.BaseFragment;
import com.hema.auction.bean.ProfileInfo;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.utils.GlideUtils;
import com.hema.auction.utils.Utils;
import com.hema.auction.widget.view.CenterItemView;
import com.hema.auction.widget.view.TitleView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * author by john
 * 个人中心
 */
public class MyFragment extends BaseFragment {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.iv_avatar)
    CircleImageView ivAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_recharge)
    TextView tvRecharge;
    @BindView(R.id.ll_auction_coin)
    LinearLayout llAuctionCoin;
    @BindView(R.id.ll_shopping_coin)
    LinearLayout llShoppingCoin;
    @BindView(R.id.ll_gift_coin)
    LinearLayout llGiftCoin;
    @BindView(R.id.tv_is_take)
    TextView tvIsTake;
    @BindView(R.id.tv_bind_phone)
    TextView tvBindPhone;
    @BindView(R.id.tv_z_balance)
    TextView tvZBalance;
    @BindView(R.id.tv_p_balance)
    TextView tvPBalance;
    @BindView(R.id.tv_g_balance)
    TextView tvGBalance;
    @BindView(R.id.ll_item)
    LinearLayout llItem;

    private String lastUid;

    @Override
    protected int getLayout() {
        return R.layout.fragment_my_center;
    }

    @Override
    protected void init() {
        super.init();
        if (!Utils.isEmpty(getProfileInfo().getId())) {
            tvId.setText("ID:" + getProfileInfo().getId());
        }

//        GlideUtils.load(this, profileInfo.getFacePic(), ivAvatar);
//        Glide.with(mContext).load("http://shanpai.51cnb.xin////uploads//face_pic//15000220096453.jpg").into(view);
        HttpManager.getInstance(getActivity()).getUserCenter(this);
        HttpManager.getInstance(getActivity()).getUserBalance(this);
        titleView.getLeftView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intentTo(SettingActivity.class);
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (getProfileInfo() != null) {
                GlideUtils.loadAvatar(getActivity(), getProfileInfo().getFacePic(), ivAvatar);
                tvName.setText(getProfileInfo().getName());
                tvId.setText("ID:" + getProfileInfo().getId());
                if (!Utils.isEmpty(profileInfo.getPhone()))
                    tvBindPhone.setText(profileInfo.getPhone());
                if (Utils.isEmpty(lastUid)) {
                    lastUid = getProfileInfo().getId();
                } else {
                    if (!lastUid.equals(getProfileInfo().getId())) {
                        lastUid = getProfileInfo().getId();
                        HttpManager.getInstance(getActivity()).getUserInfo(this);
                    }
                }
            }
            HttpManager.getInstance(getActivity()).getUserBalance(this);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        if (getProfileInfo() != null) {
            GlideUtils.loadAvatar(getActivity(), getProfileInfo().getFacePic(), ivAvatar);
            tvName.setText(getProfileInfo().getName());
            tvId.setText("ID:" + getProfileInfo().getId());
            if (!Utils.isEmpty(profileInfo.getPhone()))
                tvBindPhone.setText(profileInfo.getPhone());
        }
    }

    @OnClick({R.id.ll_profile, R.id.iv_message, R.id.ll_bind_phone, R.id.ll_difference_order, R.id.ll_my_sun
            , R.id.ll_receiving_information
            , R.id.tv_my_auction, R.id.tv_recharge, R.id.tv_property, R.id.tv_is_take, R.id.tv_taken_in, R.id.tv_missed
            , R.id.tv_payment, R.id.tv_bask_single, R.id.ll_property})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_profile:
                intentTo(MyInfoActivity.class);
                break;
            case R.id.iv_message:
                intentTo(MessageActivity.class);
                break;
            case R.id.ll_bind_phone:
                if (TextUtils.isEmpty(profileInfo.getPhone()))
                    intentTo(BindPhoneActivity.class);
                break;
            case R.id.tv_property:
            case R.id.ll_property:
                intentTo(MyPropertyActivity.class);
                break;
            case R.id.tv_my_auction:
                intentTo(MyAuctionActivity.class);
                break;
            case R.id.tv_is_take:
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.EXTRA_TAB_INDEX, 1);
                intentTo(MyAuctionActivity.class, bundle);
                break;
            case R.id.tv_taken_in:
                bundle = new Bundle();
                bundle.putInt(Constant.EXTRA_TAB_INDEX, 2);
                intentTo(MyAuctionActivity.class, bundle);
                break;
            case R.id.tv_missed:
                bundle = new Bundle();
                bundle.putInt(Constant.EXTRA_TAB_INDEX, 3);
                intentTo(MyAuctionActivity.class, bundle);
                break;
            case R.id.tv_payment:
                bundle = new Bundle();
                bundle.putInt(Constant.EXTRA_TAB_INDEX, 4);
                intentTo(MyAuctionActivity.class, bundle);
                break;
            case R.id.tv_bask_single:
                bundle = new Bundle();
                bundle.putInt(Constant.EXTRA_TAB_INDEX, 7);
                intentTo(MyAuctionActivity.class, bundle);
                break;
            case R.id.tv_recharge:
                intentTo(RechargeActivity.class);
                break;
            case R.id.ll_difference_order:
                /**
                 * 差价购买
                 *
                 */
                intentTo(DifferenceOrderActivity.class);
                break;
            case R.id.ll_my_sun:
                /**
                 * 我的晒单
                 */
                bundle = new Bundle();
                bundle.putBoolean(Constant.EXTRA_MY_SUN, true);
                intentTo(SunSquareActivity.class, bundle);
                break;
            case R.id.ll_receiving_information:
                /**
                 * 收货信息
                 */
                intentTo(AddressActivity.class);
                break;
//            case R.id.ll_new_guidelines:
//                /**
//                 * 新手指引
//                 */
//                bundle = new Bundle();
//                bundle.putString(Constant.EXTRA_TITLE, getString(R.string.new_guidelines));
//                bundle.putString(Constant.EXTRA_URL, UrlManager.H5_GUIDE);
//                intentTo(H5Activity.class, bundle);
//                break;
//            case R.id.ll_customer_service:
//                /**
//                 * 客服中心
//                 */
//                bundle = new Bundle();
//                bundle.putString(Constant.EXTRA_TITLE, getString(R.string.customer_service));
//                bundle.putString(Constant.EXTRA_URL, UrlManager.H5_SERVICE);
//                intentTo(H5Activity.class, bundle);
//                break;
//            case R.id.ll_help_center:
//                /**
//                 * 帮助中心
//                 */
//                bundle = new Bundle();
//                bundle.putString(Constant.EXTRA_TITLE, getString(R.string.help_center));
//                bundle.putString(Constant.EXTRA_URL, UrlManager.H5_HELP);
//                intentTo(H5Activity.class, bundle);
//                break;
//            case R.id.ll_turntable:
//                /**
//                 * 转盘
//                 */
//                bundle = new Bundle();
////                bundle.putString(Constant.EXTRA_TITLE, getString(R.string.turntable));
//                HashMap<String, String> params = new HashMap<>();
//                params.put("user_id", spManager.getUserId());
//                bundle.putString(Constant.EXTRA_URL, Utils.formatUrl(Constant.H5_TURNTABLE, params));
//                intentTo(H5Activity.class, bundle);
//                break;
        }
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_GET_USER_INFO:
                try {
                    if (result.getInt("errcode") == 0) {
                        ProfileInfo info = new Gson().fromJson(result.getJSONObject("data").toString(), ProfileInfo.class);
                        spManager.setUserId(info.getId());
                        aCache.put(Constant.CACHE_KEY_PROFILE, info);
                        updateProfile(info);
                        GlideUtils.loadAvatar(getActivity(), profileInfo.getFacePic(), ivAvatar);
                        tvName.setText(profileInfo.getName());
                        tvId.setText("ID:" + profileInfo.getId());
                        if (!Utils.isEmpty(profileInfo.getPhone()))
                            tvBindPhone.setText(profileInfo.getPhone());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case TAG_GET_USER_BALANCE:
                try {
                    if (result.getInt("errcode") == 0) {
                        LogUtils.i("-------TAG_GET_USER_BALANCE = "+result);
                        JSONObject data = result.getJSONObject("data");
                        profileInfo.setzBalance(data.getString("z_balance"));
                        tvZBalance.setText(profileInfo.getzBalance());
                        profileInfo.setpBalance(data.getString("p_balance"));
                        tvPBalance.setText(profileInfo.getpBalance());
                        profileInfo.setgBalance(data.getString("g_balance"));
                        tvGBalance.setText(profileInfo.getgBalance());
                        updateProfile();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case TAG_GET_USER_CENTER:
                try {
                    if (result.getInt("errcode") == 0) {
                        JSONArray jsonArray = result.getJSONArray("data");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject json = jsonArray.getJSONObject(i);
                            CenterItemView itemView = new CenterItemView(getActivity());
                            itemView.setData(json.getString("img"), json.getString("content"), json.getString("url"));
                            llItem.addView(itemView);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
